var express = require('express');
var multer = require('multer');
var db = require("../config/db");    //引入数据库封装模块
var { randomValues } = require('../public/javascripts/utils');
const fs = require("fs");
var upload = multer({ dest: 'public/uploads/' });    //设置上传文件的存储目录
var router = express.Router();

// 注册
router.post('/reg', function (req, res) {
	var username = req.body.username;
	var password = req.body.password;
	var password2 = req.body.password2;
	if (username === '') {
		res.send('<script>alert("用户名不能为空!"); history.back();</script>');
	} else if (password === '') {
		res.send('<script>alert("密码不能为空!"); history.back();</script>');
	} else if (password !== password2) {
		res.send('<script>alert("两次密码不一致!"); history.back();</script>');
	}
	// 先查询
	db.query(`SELECT * FROM user WHERE username="${username}"`, [], function (results) {
		if (results.length == 0) {
			db.query(`INSERT INTO user (username,password) VALUES ("${username}","${password}")`, [], function (results) {
				res.send('<script>alert("恭喜，注册成功");window.location.href="/page/login";</script>');
			})
		} else {
			res.send('<script>alert("用户已存在，请重新注册!"); history.back();</script>');
		}
	})
});
// 登录
router.post('/login', function (req, res) {
	var username = req.body.username;
	var password = req.body.password;
	if (username === '') {
		res.send('<script>alert("用户名不能为空!"); history.back();</script>');
	} else if (password === '') {
		res.send('<script>alert("密码不能为空!"); history.back();</script>');
	}
	db.query(`SELECT * FROM user WHERE username="${username}" AND password="${password}" `, [], function (results) {
		if (results.length == 0) {
			res.send('<script>alert("用户名或密码错误!"); history.back();</script>');
		} else {
			req.session.username = username;
			res.send('<script>alert("登录成功!!!");window.location.href="/page/index";</script>');
		}
	})
});
// 退出功能
router.get('/logout', function(req, res) {
	req.session.destroy();
	res.redirect('/page/index');
});
// 上传头像
router.post('/upload', upload.single('file'), function(req, res) {
	fs.rename('public/uploads/' + req.file.filename, 'public/uploads/' + req.session.username + '.jpg', () => {
		res.send('<script>alert("头像上传成功");window.location.href="/page/index";</script>');
	});
});

// 添加宝箱个数的功能
router.get('/addBox',(req,res)=>{
	var count = req.query.count;
	var arr = [];
	db.query(`DELETE FROM boxes`, [], function (results) {
		for(var i = 0 ; i < count ; i++){
			arr.push("1")
		}
		db.query(`INSERT INTO boxes (is_checked) VALUES ("${arr.join('"),("')}")`, [], function (results) {
			res.send({"code":0});
		})
	})
});

//添加奖品功能
router.get('/addGood',(req,res)=>{
	var good = req.query.good;
	db.query(`INSERT INTO goods (good) VALUES ("${good}")`, [], function (results) {
		res.send({"code":0});
	})
})

//删除奖品功能
router.get('/removeGood',(req,res)=>{
	var id = req.query.id;
	db.query(`DELETE FROM goods WHERE id="${id}"`, [], function (results) {
		res.send({"code":0});
	})
})

// 抽奖的功能
router.post('/winning',(req,res)=>{
	var bid = req.body.bid;
	var user = req.session.username;
	db.query(` UPDATE boxes SET is_checked = "0" WHERE id = "${bid}" `, [], function () {
		db.query(`SELECT * FROM goods`, [], function (results) {
			results = results.map(item=>item.good)
			var good = randomValues(results);
			db.query(`INSERT INTO my_goods (good,user) VALUES ("${good}","${user}")`, [], function (results) {
				res.send({"code":0 , "good" : good});
			})
		})
	})
});
module.exports = router;
